using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Drawing;

using Mighty.Lib.Syntax;
/*
 * Modified from the SyntaxRichTextBox example, by Patrik Svensson on
 * CodeProject at: http://www.codeproject.com/KB/edit/SyntaxRichTextBox.aspx
 */

namespace Mighty.Lib.Controls
{
	public class MightyRichTextBox : System.Windows.Forms.RichTextBox
	{
		
		
		
		#region Properties
        private bool m_bPaint = false;

		private Point _caretPosition;
        /// <summary>
        /// The location of the Caret inside of the text box
        /// </summary>
        public Point CaretPosition
        {
            get
            {
                return _caretPosition;
            }
        }
		
		#endregion
		
		#region ctors
		
		public MightyRichTextBox() 
		{
			
			_caretPosition = new Point(0,0);
            this.SelectionChanged += new EventHandler(SyntaxRichTextBox_SelectionChanged);
		}
		
		#endregion
		
		#region overrides
		/// <summary>
		/// WndProc .. hack to avoid flicker?
		/// </summary>
		/// <param name="m"></param>
		protected override void WndProc(ref System.Windows.Forms.Message m)
		{
			if (m.Msg == 0x00f)
			{
				if (m_bPaint)
					base.WndProc(ref m);
				else
					m.Result = IntPtr.Zero;
			}
			else
				base.WndProc(ref m);
		}

		/// <summary>
		/// OnTextChanged
		/// </summary>
		/// <param name="e"></param>
		protected override void OnTextChanged(EventArgs e)
		{
            /*
			// Calculate shit here.
			m_nContentLength = this.TextLength;

			int nCurrentSelectionStart = SelectionStart;
			int nCurrentSelectionLength = SelectionLength;

			m_bPaint = false;
			
			this.
			
			// Find the start of the current line.
			m_nLineStart = nCurrentSelectionStart;
			while ((m_nLineStart > 0) && (Text[m_nLineStart - 1] != '\n'))
				m_nLineStart--;
			// Find the end of the current line.
			m_nLineEnd = nCurrentSelectionStart;
			while ((m_nLineEnd < Text.Length) && (Text[m_nLineEnd] != '\n'))
				m_nLineEnd++;
			// Calculate the length of the line.
			m_nLineLength = m_nLineEnd - m_nLineStart;
			// Get the current line.
			m_strLine = Text.Substring(m_nLineStart, m_nLineLength);

			// Process this line.
			ProcessLine();
            */
			m_bPaint = true;
		}
		#endregion
		
		#region event handlers
        void SyntaxRichTextBox_SelectionChanged(object sender, EventArgs e)
        {
            UpdateCaretPos();
        }
		#endregion
		
		#region private member methods
		private void UpdateCaretPos() 
		{
			Point pt;
			int line, col, index;
			// get the current line
			
			index = this.SelectionStart;
			line = this.GetLineFromCharIndex(index);
			// get the caret position in pixel coordinates
			pt = this.GetPositionFromCharIndex(index);
			// now get the character index at the start of the line, and
			// subtract from the current index to get the column
			pt.X = 0;
			col = index - this.GetCharIndexFromPosition(pt);
			// finally, update the display in the status bar, incrementing the line and
			// column values so that the first line & first character position is
			// shown as "1, 1"
			
			_caretPosition.X = ++col;
			_caretPosition.Y = ++line;
			//panCaret.Text = (++line).ToString() + ", " + (++col).ToString() ;
		}
		#endregion
		
		
	}
}
